package com.liumuxian.user.service.impl;

import com.liumuxian.user.dao.AdminDao;
import com.liumuxian.user.pojo.Admin;
import com.liumuxian.user.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import util.IdWorker;

import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminDao admindao;

    @Autowired
    private IdWorker idWorker;

    //注入密码加密工具
    @Autowired
    private BCryptPasswordEncoder passwordEncoder;

    /**
     * 增加
     * @param admin
     */
    public void saveAdmin(Admin admin) {
        admin.setId( idWorker.nextId()+"" );
        admin.setState("1");
        //密码加密
        admin.setPassword(passwordEncoder.encode(admin.getPassword()));
        admindao.save(admin);
    }

    /**
     * 修改
     * @param admin
     */
    public void updateAdmin(Admin admin) {
        admindao.save(admin);
    }

    /**
     * 删除
     * @param id
     */
    public void deleteAdminById(String id) {
        admindao.deleteById(id);
    }

    /**
     * 查询全部列表
     * @return
     */
    public List<Admin> findAdminList() {
        return admindao.findAll();
    }

    /**
     * 根据ID查询实体
     * @param id
     * @return
     */
    public Admin findAdminById(String id) {
        return admindao.findById(id).get();
    }

    /**
     * 根据条件查询列表
     * @param whereMap
     * @return
     */
    public List<Admin> findAdminList(Map whereMap) {
        //构建Spec查询条件
        Specification<Admin> specification = getAdminSpecification(whereMap);
        //Specification条件查询
        return admindao.findAll(specification);
    }

    /**
     * 组合条件分页查询
     * @param whereMap
     * @param page
     * @param size
     * @return
     */
    public Page<Admin> findAdminListPage(Map whereMap, int page, int size) {
        //构建Spec查询条件
        Specification<Admin> specification = getAdminSpecification(whereMap);
        //构建请求的分页对象
        PageRequest pageRequest =  PageRequest.of(page-1, size);
        return admindao.findAll(specification, pageRequest);
    }

    /**
     * 根据参数Map获取Spec条件对象
     * @param searchMap
     * @return
     */
    private Specification<Admin> getAdminSpecification(Map searchMap) {

        return (Specification<Admin>) (root, query, cb) ->{
            //临时存放条件结果的集合
            List<Predicate> predicateList = new ArrayList<Predicate>();
            //属性条件
            // ID
            if (searchMap.get("id")!=null && !"".equals(searchMap.get("id"))) {
                predicateList.add(cb.like(root.get("id").as(String.class), "%"+(String)searchMap.get("id")+"%"));
            }
            // 登陆名称
            if (searchMap.get("account")!=null && !"".equals(searchMap.get("account"))) {
                predicateList.add(cb.like(root.get("account").as(String.class), "%"+(String)searchMap.get("account")+"%"));
            }
            // 密码
            if (searchMap.get("password")!=null && !"".equals(searchMap.get("password"))) {
                predicateList.add(cb.like(root.get("password").as(String.class), "%"+(String)searchMap.get("password")+"%"));
            }
            // 状态
            if (searchMap.get("state")!=null && !"".equals(searchMap.get("state"))) {
                predicateList.add(cb.like(root.get("state").as(String.class), "%"+(String)searchMap.get("state")+"%"));
            }

            //最后组合为and关系并返回
            return cb.and( predicateList.toArray(new Predicate[predicateList.size()]));
        };

    }

    /**
     * 管理员登录
     * @param account
     * @param password
     * @return
     */
    public Admin findAdminByAccountAndPassword(String account,String password){
        //1.根据手机号码查询用户
        Admin admin = admindao.findByAccount(account);
        //判断用户是否存在
        if(null == admin){
            //用户不存在
            return null;
        }

        //2.判断密码是否正确
        if(!passwordEncoder.matches(password,admin.getPassword())){
            //密码不匹配
            return null;
        }

        //3.返回登录成功的用户
        return admin;

    }
}
